﻿using System;
using System.Net;
using System.Windows;
using SimpleJson;
using System.Collections.Generic;

namespace MetroEventbrite.External.Eventbrite
{

    public class EBAttendee
    {
        public static EBAttendee Load(string json)
        {
            var result = new EBAttendee();
            JsonObject jsonObj;
            try
            {

                jsonObj = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(json);
            }
            catch (Exception ex)
            {
                return result;
            }
            return Load(jsonObj);
        }

        public static EBAttendee Load(JsonObject jsonObj)
        {
            var result = new EBAttendee();

            if (jsonObj.ContainsKey("id"))
            {
                result.EventId = jsonObj["id"].ToString();
            }
            if (jsonObj.ContainsKey("ticket_id"))
            {
                result.TicketId = jsonObj["ticket_id"].ToString();
            }
            if (jsonObj.ContainsKey("quantity"))
            {
                result.NumberOfTicketsPurchased = Convert.ToInt32(jsonObj["quantity"].ToString());
            }
            if (jsonObj.ContainsKey("barcode"))
            {
                result.Barcodes = jsonObj["barcode"].ToString().Split(',');
            }
            if (jsonObj.ContainsKey("order_id"))
            {
                result.OrderId = jsonObj["order_id"].ToString().Split(',');
            }
            if (jsonObj.ContainsKey("order_type"))
            {
                result.OrderType = jsonObj["order_type"].ToString();
            }
            if (jsonObj.ContainsKey("created"))
            {
                result.CreateDateString = jsonObj["created"].ToString();
            }
            if (jsonObj.ContainsKey("modified"))
            {
                result.ModifiedDateString = jsonObj["modified"].ToString();
            }
            if (jsonObj.ContainsKey("event_date"))
            {
                result.EventDateChosenString = jsonObj["event_date"].ToString();
            }
            if (jsonObj.ContainsKey("discount"))
            {
                result.DiscountCodeInfo = jsonObj["discount"].ToString();
            }
            if (jsonObj.ContainsKey("notes"))
            {
                result.Notes = jsonObj["notes"].ToString();
            }
            if (jsonObj.ContainsKey("email"))
            {
                result.EmailAddress = jsonObj["email"].ToString();
            }
            if (jsonObj.ContainsKey("prefix"))
            {
                result.NamePrefix = jsonObj["prefix"].ToString();
            }
            if (jsonObj.ContainsKey("first_name"))
            {
                result.FirstName = jsonObj["first_name"].ToString();
            }
            if (jsonObj.ContainsKey("last_name"))
            {
                result.LastName = jsonObj["last_name"].ToString();
            }
            if (jsonObj.ContainsKey("suffix"))
            {
                result.NameSuffix = jsonObj["suffix"].ToString();
            }
            if (jsonObj.ContainsKey("home_address") || jsonObj.ContainsKey("home_address_2") || jsonObj.ContainsKey("home_city") ||
                jsonObj.ContainsKey("home_postal_code") || jsonObj.ContainsKey("home_region") || jsonObj.ContainsKey("home_country") ||
                jsonObj.ContainsKey("home_country_code"))
            {
                result.HomeMailingAddress = new EBMailingAddress();
                if (jsonObj.ContainsKey("home_address"))
                {
                    result.HomeMailingAddress.Address = jsonObj["home_address"].ToString();
                }
                if (jsonObj.ContainsKey("home_address_2"))
                {
                    result.HomeMailingAddress.Address2 = jsonObj["home_address_2"].ToString();
                }
                if (jsonObj.ContainsKey("home_city"))
                {
                    result.HomeMailingAddress.City = jsonObj["home_city"].ToString();
                }
                if (jsonObj.ContainsKey("home_postal_code"))
                {
                    result.HomeMailingAddress.PostalCode = jsonObj["home_postal_code"].ToString();
                }
                if (jsonObj.ContainsKey("home_region"))
                {
                    result.HomeMailingAddress.Region = jsonObj["home_region"].ToString();
                }
                if (jsonObj.ContainsKey("home_country"))
                {
                    result.HomeMailingAddress.Country = jsonObj["home_country"].ToString();
                }
                if (jsonObj.ContainsKey("home_country_code"))
                {
                    result.HomeMailingAddress.CountryCode = jsonObj["home_country_code"].ToString();
                }
            }
            if (jsonObj.ContainsKey("home_phone"))
            {
                result.HomePhone = jsonObj["home_phone"].ToString();
            }

            if (jsonObj.ContainsKey("cell_phone"))
            {
                result.CellPhone = jsonObj["cell_phone"].ToString();
            }
            if (jsonObj.ContainsKey("ship_address") || jsonObj.ContainsKey("ship_address_2") || jsonObj.ContainsKey("ship_city") ||
                jsonObj.ContainsKey("ship_postal_code") || jsonObj.ContainsKey("ship_region") || jsonObj.ContainsKey("ship_country") ||
                jsonObj.ContainsKey("ship_country_code"))
            {
                result.ShippingAddress = new EBMailingAddress();
                if (jsonObj.ContainsKey("ship_address"))
                {
                    result.ShippingAddress.Address = jsonObj["ship_address"].ToString();
                }
                if (jsonObj.ContainsKey("ship_address_2"))
                {
                    result.ShippingAddress.Address2 = jsonObj["ship_address_2"].ToString();
                }
                if (jsonObj.ContainsKey("ship_city"))
                {
                    result.ShippingAddress.City = jsonObj["ship_city"].ToString();
                }
                if (jsonObj.ContainsKey("ship_postal_code"))
                {
                    result.ShippingAddress.PostalCode = jsonObj["ship_postal_code"].ToString();
                }
                if (jsonObj.ContainsKey("ship_region"))
                {
                    result.ShippingAddress.Region = jsonObj["ship_region"].ToString();
                }
                if (jsonObj.ContainsKey("ship_country"))
                {
                    result.ShippingAddress.Country = jsonObj["ship_country"].ToString();
                }
                if (jsonObj.ContainsKey("ship_country_code"))
                {
                    result.ShippingAddress.CountryCode = jsonObj["ship_country_code"].ToString();
                }
            }
            if (jsonObj.ContainsKey("work_address") || jsonObj.ContainsKey("work_address_2") || jsonObj.ContainsKey("work_city") ||
                jsonObj.ContainsKey("work_postal_code") || jsonObj.ContainsKey("work_region") || jsonObj.ContainsKey("work_country") ||
                jsonObj.ContainsKey("work_country_code"))
            {
                result.WorkAddress = new EBMailingAddress();
                if (jsonObj.ContainsKey("work_address"))
                {
                    result.WorkAddress.Address = jsonObj["work_address"].ToString();
                }
                if (jsonObj.ContainsKey("work_address_2"))
                {
                    result.WorkAddress.Address2 = jsonObj["work_address_2"].ToString();
                }
                if (jsonObj.ContainsKey("work_city"))
                {
                    result.WorkAddress.City = jsonObj["work_city"].ToString();
                }
                if (jsonObj.ContainsKey("work_postal_code"))
                {
                    result.WorkAddress.PostalCode = jsonObj["work_postal_code"].ToString();
                }
                if (jsonObj.ContainsKey("work_region"))
                {
                    result.WorkAddress.Region = jsonObj["work_region"].ToString();
                }
                if (jsonObj.ContainsKey("work_country"))
                {
                    result.WorkAddress.Country = jsonObj["work_country"].ToString();
                }
                if (jsonObj.ContainsKey("work_country_code"))
                {
                    result.WorkAddress.CountryCode = jsonObj["work_country_code"].ToString();
                }
            }
            if (jsonObj.ContainsKey("work_phone"))
            {
                result.WorkPhone = jsonObj["work_phone"].ToString();
            }
            if (jsonObj.ContainsKey("job_title"))
            {
                result.Title = jsonObj["job_title"].ToString();
            }
            if (jsonObj.ContainsKey("company"))
            {
                result.CompanyName = jsonObj["company"].ToString();
            }
            if (jsonObj.ContainsKey("website"))
            {
                result.Website = jsonObj["website"].ToString();
            }
            if (jsonObj.ContainsKey("blog"))
            {
                result.Blog = jsonObj["blog"].ToString();
            }
            if (jsonObj.ContainsKey("gender"))
            {
                result.GenderString = jsonObj["gender"].ToString();
            }
            if (jsonObj.ContainsKey("birth_date"))
            {
                result.BirthDateString = jsonObj["birth_date"].ToString();
            }
            if (jsonObj.ContainsKey("age"))
            {
                result.Age = Convert.ToInt16(jsonObj["age"].ToString());
            }
            if (jsonObj.ContainsKey("answers") && jsonObj["answers"] != null)
            {
                result.AnswersRawString = jsonObj["answers"].ToString();

                result.Answers = new List<EBAttendeeAnswer>();
                var answers = (JsonArray)jsonObj["answers"];
                foreach (var item in answers)
                {
                    var data = (JsonObject)item;
                    result.Answers.Add(EBAttendeeAnswer.Load(data));
                }
            }
            return result;
        }

        public string EventId { get; set; }

        public string TicketId { get; set; }

        public int NumberOfTicketsPurchased { get; set; }

        public string CurrencyType { get; set; }

        public string[] Barcodes { get; set; }

        public string[] OrderId { get; set; }

        public string OrderType { get; set; }

        public string CreateDateString { get; set; }

        public string ModifiedDateString { get; set; }

        public string EventDateChosenString { get; set; }

        public string DiscountCodeInfo { get; set; }

        public string Notes { get; set; }

        public string EmailAddress { get; set; }

        public string NamePrefix { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string NameSuffix { get; set; }

        public EBMailingAddress HomeMailingAddress { get; set; }

        public string HomePhone { get; set; }

        public string CellPhone { get; set; }

        public EBMailingAddress ShippingAddress { get; set; }

        public EBMailingAddress WorkAddress { get; set; }

        public string WorkPhone { get; set; }

        public string Title { get; set; }

        public string CompanyName { get; set; }

        public string Website { get; set; }

        public string Blog { get; set; }

        public string GenderString { get; set; }

        public string BirthDateString { get; set; }

        public short Age { get; set; }

        public string AnswersRawString { get; set; }

        public List<EBAttendeeAnswer> Answers { get; set; }
    }
}
